<?php
/**
 * 铅球图
 *
 * PHP version 5
 *
 * @package   Ecga
 * @author    Farmer.Li <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   SVN: $Id$
 */
namespace Project\Apps\Ecga\Library\FusionCharts;

use \Project\Apps\Ecga\Library\FusionCharts\ChartXml;
use \Exception;

/**
 * 铅球图
 *
 * PHP version 5
 *
 * @package   Ecga
 * @author    Farmer.Li <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   Release: <0.2>
 */
class Bubble extends ChartXml
{
	
	/**
	 * 构造函数
	 *
	 * @param string $caption   图表说明
	 * @param string $xAxisName x轴别名
	 * @param string $yAxisName y轴别名
	 *
	 * @return void
	 */
	public function __construct($caption, $xAxisName, $yAxisName)
	{
		parent::__construct($caption);
		$this->setStyle(
			array(
				'palette' => "3",
				'is3D' => "1",
				'animation' => "1",
				'clipBubbles' => "1",
				'xAxisMaxValue' => "100",
				'showPlotBorder' => "0",
				'xAxisName' => $xAxisName,
				'yAxisName' => $yAxisName,
				'chartRightMargin' => "30",
			)
		);
		$this->data['__child']['categories'] = array(
			'tagName' => 'categories'
		);
	}
	/**
	 * 设置图表数据
	 *
	 * @param array  $xData x轴格式化数组
	 * @param array  $yData y轴格式化数组
	 * @param array  $zData z轴格式化数组
	 * @param string $color 颜色
	 * 
	 * @return object
	 */
	public function setData($xData, $yData, $zData, $color = '')
	{
		$maxX = max($xData);
		$minX = min($xData);
		$this->data['__attr']['xAxisMaxValue'] = $max;
		$slice = ($maxX - $minX) / 10;
		for ($i = 0; $i <= 7; $i++) {
			$value = $i * $slice;
			$this->data['__child']['categories']['__child'][] = array(
				'tagName' => 'category',
				'__attr' => array(
					'label' => ($value * 100) . '%',
					'x' => $value * 100,
					'showVerticalLine' => $i == 0 ? 0 : 1
				)
			);
			
		}
		$this->data['__child']['dataSet'] = array(
			'tagName' => 'dataSet',
			'__attr' => array(
				//'showValues' => "1",
				//'showNames' => '1'
			)
		);
		foreach ($xData as $key => $value) {
			$value = $value ? $value * 100 : 0;
			$this->data['__child']['dataSet']['__child'][] = array(
				'tagName' => 'set',
				'__attr' => array(
					'x' => $value,
					'y' => $yData[$key],
					'z' => $zData[$key],
					'name' => $key,
					'color' => $this->randColor(),
					'tooltext' => "{$key}: 转化率 {$value}%, UV {$yData[$key]}, 平均客单价 {$zData[$key]}"
				)
			);
		}
		$midX = $maxX / 2 * 100;
		$midY = max($yData) / 2;
		$this->data['__child']['trendlines'] = array(
			'tagName' => 'trendlines',
			'__child' => array(
				array(
					'tagName' => 'line',
					'__attr' => array(
						'startValue' => $midY,
						'isTrendZone' => 0,
						'displayValue' => 'middle Y : ' . $midY,
						'color' => '0372AB'
					),
				)
			)
		);
		$this->data['__child']['vTrendlines'] = array(
			'tagName' => 'vTrendlines',
			'__child' => array(
				array(
					'tagName' => 'line',
					'__attr' => array(
						'startValue' => $midX,
						'endValue' => $midX,
						'isTrendZone' => 0,
						'displayValue' => 'middle X : ' . $midX . '%',
						'color' => '0372AB'
					)
				)
			)
		);
	}
}
?>